import { views } from '@/views';
import type { RouteRecordRaw } from 'vue-router';
import { BlankLayout, BasicLayout } from '~/src/layout';
import type { newTree } from '@/utils';
import { AuthRoute } from '~/src/typings/router';
export const transformRouteToVue = (routers: AuthRoute.Route[]) => {
  return routers.map(item => transformRouteToVueItem(item));
};

export const transformRouteToVueItem = (item: AuthRoute.Route) => {
  const router = {...item} as RouteRecordRaw;

  // 判断动态路由
  if(isDynamicPath(item)){
    Object.assign(router, {path: router.meta?.dynamicPath});
  }

  //判断components并指向真实地址
  if(isComponents(item)){
    const actions:any = {
      blank(){
        router.component = getViewsComponents(item);
      },
      basic(){
        router.component = getlayoutsComponents('basic');
      },
      self(){
        router.component = getViewsComponents(item);
      }
    };
    try{
      actions[item.component!]();
    }catch{
      console.error('赋值component地址出错');
    }
  }

  //判断是否为菜单独立路由并添加父级
  if(isSingle(item)){
    const parentPath = `${router.path}-parent`;
    const parent = {
      path: parentPath,
      redirect: item.path,
      component: getlayoutsComponents('basic'),
      children: [router]
    };
    return parent;
  }
  //判断子路由
  if(isRouterChildren(item)){
    router.children = transformRouteToVue(router.children as AuthRoute.Route[]);
  }

  return router;
};

export const filterRouterToAuth = (routers?: AuthRoute.Route[], authRouter?: newTree[]): any => {
  return routers?.map(item => {
    const { name, meta } = item;
    //匹配的路由
    const mateRouter = authRouter?.find((key) => key?.name === name);
    if(mateRouter){
      const route = {...item};
      if(meta?.btnAuth){
        (route.meta!).btnAuth = mateRouter?.children as string[];
      }
      if(isRouterChildren(item)){
        route.children = filterRouterToAuth(item?.children, mateRouter?.children);
      }
      return route;
    }
  }).filter(item => Boolean(item));
};


export const isDynamicPath = (route:AuthRoute.Route) => {
  return Boolean(route?.meta?.dynamicPath);
};

export const isComponents = (route:AuthRoute.Route) => {
  return Boolean(route?.component);
};


export const isRouterChildren = (route:AuthRoute.Route) => {
  return Boolean(route?.children);
};

export const isSingle = (route:AuthRoute.Route) => {
  return Boolean(route?.meta?.single);
};

/**
 * 获取页面导入的vue文件(懒加载的方式)
 * @param route - 路由信息
 */
export const getViewsComponents = ( router:AuthRoute.Route) => {
  const routeKey = router?.name;
  if(!routeKey){
    console.error('该路由没有名称，无法解析');
  }else {

    return views[routeKey];
  }
};
/**
 * 获取组件渲染地址
 * @param routeType - 组件类型
 */
export const getlayoutsComponents = (routerType:AuthRoute.Route['component']) => {
  const actios:any = {
    blank:BlankLayout,
    basic:BasicLayout
  };
  return actios[routerType!];
};

/** 将路由名字转换成路由路径 */
export function transformRouteNameToRoutePath(
  name: Exclude<AuthRoute.RouteKey, 'not-found-page'>
): AuthRoute.RoutePath {
  const rootPath: AuthRoute.RoutePath = '/';
  if (name === 'root') return rootPath;

  const splitMark: AuthRoute.RouteSplitMark = '_';
  const pathSplitMark = '/';
  const path = name.split(splitMark).join(pathSplitMark);

  return (pathSplitMark + path) as AuthRoute.RoutePath;
}
